<?php

namespace app\api\controller;

use think\Db;

//个人中心
class User extends BaseController
{
    //个人中心
    public function center()
    {
        $user_info = session('user_info');
        $data['user_info']['avatar'] = $user_info['avatar'];
        $data['user_info']['name'] = $user_info['name'];
        $data['user_info']['exp'] = intval($user_info['exp']);
        $data['user_info']['level'] = $user_info['level'];

        $data['money']['allow_withdraw'] = round($user_info['balance'], 2);
        $data['money']['not_settlemetn'] = round($user_info['not_settlemetn'], 2);

        $address = Db::name('address')->where(['user_id' => $user_info['id'], 'type' => 1])->find();
        $address_count = Db::name('address')->where(['user_id' => $user_info['id'], 'type' => ['neq', 1]])->count();
        $data['address']['sender'] = $address['sender'];
        $data['address']['address_count'] = $address_count;

        $data['gang']['fans_count'] = intval($user_info['fans_count']);
        $gang = Db::name('gang')->find($user_info['gang_id']);
        $data['gang']['avatar'] = $gang['avatar'];
        $data['gang']['name'] = $gang['name'];
        $data['gang']['gang_id'] = $user_info['gang_id'];
        $data['gang']['member_count'] = intval($gang['member_num']);

        return success($data);
    }

    // 获取用户余额
    public function getBalance()
    {
        $user_info = session('user_info');
        $user_db_info = Db::name('user')->find($user_info['id']);
        $data['allow_withdraw'] = round($user_db_info['balance'], 2);
        $data['not_settlemetn'] = round($user_db_info['not_settlemetn'], 2);

        return success($data);
    }

    // 个人账户余额 提现 每个人每天就只能提现一次
    public function withdraw()
    {
        $user_info = session('user_info');
        $money = input('param.money', 1);
        $form_id = input('param.form_id', '');
        if (!$form_id) {
            return error('参数错误：错误的form_id');
        }
        $money = round($money, 2);
        $limit = cache('withdraw_user_id'.$user_info['id']);
        if ($limit) {
            return error('每个人每天就只能提现一次！');
        }

        if ($money < 1.1) {
            return error('单次提现金额不能小于1.1元！');
        }
        if ($money > 20000) {
            return error('为了您的支付安全单次提现金额不能大于2万');
        }

        $user_db_info = Db::name('user')->find($user_info['id']);
        $user_db_info['balance'] = round($user_db_info['balance'], 2);
        if ($user_db_info['balance'] < $money) {
            return error('提现金额不能大于账户余额');
        }
        if (!$user_db_info['openid']) {
            return error('请先绑定微信');
        }
        cache('withdraw_user_id'.$user_db_info['id'], 1, strtotime('tomorrow') - time());
        cache('api_user_info_'.$user_info['id'], null);
        $time = time();
        // 提现记录
        $new_id = Db::name('user_finance')->insert(['user_id' => $user_db_info['id'], 'money' => -$money, 'ct' => $time, 'type' => '提现', 'status' => 0]);
        // 微信自动支付
        $wx_service = new \wx\WeixinService();
        // 1%手续费
        $pay_money = round($money * 0.99, 2);
        $row = ['id' => $new_id, 'openid' => $user_db_info['openid'], 'desc' => '提现', 'pay_money' => $pay_money];
        $result = $wx_service->serverAutoPay($row);
        if ('SUCCESS' == $result['return_code'] && 'SUCCESS' == $result['result_code']) {
            // 减余额 加累计提现
            Db::name('user')->where('id', $user_db_info['id'])->update(['balance' => round($user_db_info['balance'] - $money, 2),'withdraw' =>round($user_db_info['withdraw'] + $money, 2)]);
            Db::name('user_finance')->where('id', $result['partner_trade_no'])->update(['status' => 1, 'pay_time' => time(), 'payment_no' => $result['payment_no']]);

            if ($user_db_info['gzh_openid']) {
                // 发送模板消息 提现 公众号
                $wxgzh_service = new \wx\WxgzhService();
                $data = [
                    'first' => ['value' => '提现成功'],
                    'keyword1' => ['value' => date('Y-m-d H:i:s', $time)],
                    'keyword2' => ['value' => '微信钱包'],
                    'keyword3' => ['value' => $money.'元'],
                    'keyword4' => ['value' => '1%'],
                    'keyword5' => ['value' => $pay_money.'元'],
                    'remark' => ['value' => '感谢你的使用。'],
                ];
                $wxgzh_service->sendTemplateMsg($user_db_info['gzh_openid'], 'MZNMUsQwilGiq6uZ4VpEYt35kgLKx8L5zY3r_69LFLk', $data, 'pages/index/welcome');
            } elseif ($user_db_info['openid']) {
                // 发送模板消息 提现 小程序
                $wx_service = new \wx\WeixinService();
                $tData = [
                    'keyword1' => ['value' => $user_db_info['name']],
                    'keyword2' => ['value' => $money.'元'],
                    'keyword3' => ['value' => '1%'],
                    'keyword4' => ['value' => $pay_money.'元'],
                    'keyword5' => ['value' => date('Y-m-d H:i:s', time())],
                ];
                $wx_service->sendTemplateMsg($user_db_info['openid'], $form_id, '3Q6b6ORnDwN96JY1_LsM9UTxTVzhfMDkNJ-upBTC7ws', 'pages/index/welcome', $tData);
            }

            return success($result);
        }

        return error($result['result_code']);
    }

    // 获取用户粉丝列表
    public function getFansList()
    {
        $user_info = session('user_info');
        $data = Db::name('user')->where('inviter_id', $user_info['id'])->field('id,name,avatar')->select();

        return success($data);
    }
}
